Evil solution


In [1]:
evil = lambda d,p=[0,0],o=0:sum(abs(a) for a in p) if len(d)==0 else evil(d[1:],p=[a+int(d[0][1:])*b for a,b in zip(p,[(1,0),(0,-1),(-1,0),(0,1)][[[1,3],[2,0],[3,1],[0,2]][o][0 if d[0][0]=='L' else 1]])],o=[[1,3],[2,0],[3,1],[0,2]][o][0 if d[0][0]=='L' else 1])

Test examples


In [2]:
evil(["R2", "L3"])


Out[2]:
5

In [3]:
evil(["R2", "R2", "R2"])


Out[3]:
2

In [4]:
evil(["R5", "L5", "R5", "R3"])


Out[4]:
12

Process input


In [5]:
with open("inputs/day1a.txt") as fd:
    print("Easter Bunny HQ is {} blocks away".format(evil([i.strip() for i in fd.read().split(',')])))


Easter Bunny HQ is 291 blocks away

In [ ]: